﻿@model  DHQGHN.DAL.Common.DataPage<DHQGHN.PM0.Models.Admin.UserInRoleModel>
@{
    ViewBag.Title = ViewBag.CurrentMenuName;
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<div class="content">
    <div class="clear"></div>
    <table style="width: 100%">
        <tr>
            <td style="height: 40px;">
                <div id="nav"><a href="/UserInRole/Index" title="">@ViewBag.Title.ToUpper()</a></div>
            </td>
        </tr>
        <tr>
            <td style="">
                <div style="width: 90%;">

                    <div>
                        <div class="col-md-2" style="text-align: right; padding: 4px 4px 4px 4px; ">
                            Tên ứng dụng
                        </div>
                        <div>
                            <div class="col-md-4">
                                @Html.DropDownList("AppID", new SelectList(ViewBag.lstApp, "value", "text", 0), new { @id = "input-code-app", @name = "code", @class = "SelectBoxContent", @placeholder = "" })
                            </div>
                        </div>
                        <div class="col-md-2" style="text-align: right; padding: 4px">
                            Tên
                        </div>
                        <div class="col-md-4">
                            <input type="text" class="txtsearch TextBoxContent" style="width: 100% !important" id="input-name" name="NAME">
                        </div>
                    </div>
                    <div>
                        <div class="col-md-2" style="text-align: right; padding: 4px 4px 4px 4px; ">
                            Tên vai trò
                        </div>
                        <div>
                            <div class="col-md-4" id="rolelist">
                                @*@Html.DropDownList("RoleID", new SelectList(ViewBag.lstRole, "value", "text", 0), new { @id = "input-code", @name = "code", @class = "SelectBoxContent", @placeholder = "" })*@
                                <select class="input-validation-error SelectBoxContent" id="input-code" name="RoleID" disabled="disabled" placeholder="">
                                    <option value="0">Chọn vai trò</option>

                                </select>
                            </div>
                        </div>
                    </div>
                    <div>
                        <div style="height: 10px;"></div>
                        <div class="col-md-12">

                            <div style="padding-top: 10px; text-align: center">
                                <div class="col-md-12">
                                    <span class="input-user-btn">
                                        <button class="btn btn-primary" type="button" onclick="return btnSearchClick();">
                                            <span class="glyphicon glyphicon-search"></span>Tìm kiếm
                                        </button>

                                        <button type="button" id="user-in-role-add" style="margin-left: 10px;" class="btn btn-primary"
                                                value="Thêm mới">
                                            <span class="glyphicon glyphicon-plus"></span>Gán vai trò
                                        </button>

                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="user-in-role-list" style="margin-top:10px;">
                    @{
                        Html.RenderPartial("_UserInRoleList");
                    }
                </div>
            </td>
        </tr>
    </table>
</div>

<script type="text/javascript">
    function btnSearchClick() {
        OnCriteria(0);
    }

    $(document).ready(function () {
        $('#user-in-role-add').bind('click', function (event) {
            if ($("#input-code").val().trim() > 0) {
                functionCreate(-1, $("#input-code").val().trim(), "Gán vai trò");
            } else {
                warning("Bạn cần chọn vai trò để thêm người dùng", "Phân vai trò cho người dùng");
            }
        });
        $('#input-code-app').change(function () {
            var appid = $('#input-code-app').val();
            var $catlist = $('#rolelist');
            $.ajax({
                dataType: "json",
                type: "POST",
                //type: "GET",
                url: '/UserInRole/GetListRoleByID',
                contentType: "application/json",
                //data: { key: ctrSearch, departmentId: ctrdepartmentId, pageIndex: pageIndex },
                data: JSON.stringify({ appId: appid }),
                success: function (data) {
                    var result = '';
                    for (var i = 0, iL = data.length; i < iL; i++) {
                        result += '<option value="' + data[i].Value + '">' + data[i].Text + '</option>';
                    }
                    $("#input-code").html(result);
                    $("#input-code").removeAttr("disabled"); 
                }
            });
        });
        $('#input-code').change(function () {
            OnCriteria(crrPageIndex);
        });
        $('#input-code-app').change(function () {
            if ($('#input-code-app').val() == 0) {
                $('#input-code').val('0');
            }
            OnCriteria(crrPageIndex);
        });
        function functionCreate(userInRoleID, roleID, titleForm) {
            
            $('#user-in-role-model-container')
              .load("/UserInRole/actionPrepareCreateOrUpdateUserInRole", { id: userInRoleID, roleID: roleID, appid: $('#input-code-app').val() }, function () {
                  $(this).dialog({
                      draggable: true,
                      modal: true,
                      resizable: false,
                      width: 'auto',
                      title: titleForm,
                      width: '50%',
                      maxheight: 480,
                      my: "center",
                      at: "center",
                      of: window,
                      buttons: [
                            {
                                text: "Lưu",
                                icons: {
                                    primary: "ui-icon-heart"
                                },
                                class: 'btn btn-primary',
                                click: function () {
                                    var values = getListUserInRoleID()
                                    if (values == "") {
                                        warning("Bạn cần chọn người dùng", "Thông báo");
                                    }
                                    else {
                                        if (save) {
                                            var result = save();
                                            if (result) {
                                                $(this).dialog("destroy");
                                                $('#user-in-role-model-container').hide();
                                            }
                                        }
                                    }                                    
                                }
                            },
                            {
                                text: "Đóng",
                                icons: {
                                    primary: "ui-icon-close"
                                },
                                class: 'btn btn-primary',
                                click: function () {                                    
                                    $(this).dialog("destroy");
                                    $('#user-in-role-model-container').hide();
                                }

                            }
                      ]
                  });
              });
        }

    });
</script>